package com.example.zhou.androidmusic;

import android.graphics.Color;
import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.zhou.adapter.Decoration.MyDecoration;
import com.example.zhou.adapter.MusicInfoAdapter;
import com.example.zhou.filehelper.AlbumUtil;
import com.example.zhou.musicmodel.MusicBean;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AlbumInfoActivity extends AppCompatActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.iv_album)
    ImageView iv_album;
    @BindView(R.id.nsv)
    NestedScrollView nsv_rev;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<MusicBean> musicBeen = new ArrayList<>();
    private MusicInfoAdapter adapter;
    private LinearLayoutManager layoutManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_album_info);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        toolbar.setTitle("");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        defaultSetting();
        setData();
        getAlbum(musicBeen);
        setRecyclerView();
    }


    public void defaultSetting() {
        toolbar.setBackgroundColor(Color.argb(24, 0, 0, 255));
    }

    private void getAlbum(List<MusicBean> list) {
        byte[] bytes = AlbumUtil.getMusicAlbum(new File(list.get(0).getFilepath()));
        if (bytes != null)
            Glide.with(this).load(bytes).into(iv_album);
    }


    private void setData() {
        musicBeen = (List<MusicBean>) getIntent().getExtras().getSerializable("MUSICBEAN");
        Collections.sort(musicBeen);
    }

    private void setRecyclerView() {
        adapter = new MusicInfoAdapter(this, musicBeen);
        layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(layoutManager);
        MyDecoration myDecoration = new MyDecoration(this, LinearLayoutManager.VERTICAL);
        recyclerView.addItemDecoration(myDecoration);
    }

}
